define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'highproject/index' + location.search,
                    add_url: 'highproject/add',
                    edit_url: 'highproject/edit',
                    del_url: 'highproject/del',
                    table: 'highproject',
                },
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                ortName: 'status desc,id desc',
                undefinedText:'',
                // search:false,
                showToggle:false,
                singleSelect:true,
                 //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedNumber: 5,
                columns: [
                    [
                        {checkbox: true,width:'10px',},
                        {field: '', title: __('序号'),operate:false,table: table,width:'20px',formatter:Table.api.formatter.natureorder},
                        // {field: 'pno', title: __('Pno')},
                        // {field: 'sort', title: __('Sort')},
                        // {field: 'street_id', title: __('Street_id')},
                        // {field: 'street_name', title: __('Street_name')},
                        {field: 'name', title: __('Name'),operate:'like',width:'120px'},
                        {field: 'chargedep_names', title: __('提报部门'),width:'80px',operate:false,
                            formatter:Table.api.formatter.delimiter
                        },
                        {field: 'chargedep_ids', title: __('提报部门'),visible:false,
                            operate:'FIND_IN_SET',
                            searchList: $.getJSON("selectlist/index/tablename/chargedep")
                        },
                        {field: 'five_industry_id', title: __('Five_industry_id'),
                            width:'120px',
                            visible:false,
                            searchList: $.getJSON("selectlist/index/tablename/five_industry")
                        },
                        {field: 'industry_category_ids', title: __('Industry_category_ids'),
                            visible:false,
                            searchList: $.getJSON("selectlist/index/tablename/industry_category")
                        },
                        {field: 'industry_category_names', width:'80px',title: __('industry_category_ids'),
                            operate:false,
                            formatter:function(value,row,index){
                                if(value)
                                {
                                    str = value.replace(/都不是/g,"");
                                    str = str.replace(/,/g,"/");
                                    str = str?'/'+str:str;
                                    return row.five_industry_name + str;
                                }
                                else
                                    return row.five_industry_name;

                            }
                        },
                        {field: 'registered_capital', title: __('Registered_capital'),
                            width:'50px',
                            operate:'BETWEEN',
                            formatter: Table.api.formatter.price
                        },
                        {field: 'investment_total', title: __('Investment_total'),
                            width:'50px',
                            operate:'BETWEEN',
                            formatter:function(value,row,index) {
                                if(value == -100)
                                    return '待定';
                                else if(value > 0)
                                    return parseFloat(value);
                                else
                                    return '';
                            }
                        },
                        {field: 'domestic_capital', title: __('Domestic_capital'),
                            width:'50px',
                            operate:'BETWEEN',
                            formatter: Table.api.formatter.price
                        },
                        {field: 'foreign_capital', title: __('Foreign_capital'),
                            width:'50px',
                            operate:'BETWEEN',
                            formatter: Table.api.formatter.price
                        },
                        {field: 'framework_agreement_time_desc', title: __('framework_agreement_time_desc'),
                            width:'80px',
                            operate:false
                        },
                        {field: 'framework_agreement_time', title: __('framework_agreement_time_desc'),
                            visible:false,
                            addclass:'datetimerange',
                            datetimeFormat:"YYYY-MM-DD",
                        },
                        {field: 'invesment_agreement_time_desc',
                            width:'80px',
                            title: __('invesment_agreement_time_desc'),
                            operate:false
                        },
                        {field: 'invesment_agreement_time', title: __('invesment_agreement_time'),
                            visible:false,
                            addclass:'datetimerange',
                            datetimeFormat:"YYYY-MM",
                        },
                        {field: 'register_time', title: __('register_time'),
                            addclass:'datetimerange',
                            datetimeFormat:"YYYY-MM",
                            formatter: Table.api.formatter.datetime,
                            visible:false
                        },
                        {field: 'register_time_desc', title: __('register_time'),
                            width:'80px',
                            operate:false
                        },
                        {field: 'is_land', title: __('是否拿地'),
                           formatter:Table.api.formatter.radio
                        },
                        {field: 'land_time', title: __('land_time'),
                            addclass:'datetimerange',
                            datetimeFormat:"YYYY-MM",
                            formatter: Table.api.formatter.datetime,
                            visible:false
                        },
                        {field: 'land_time_desc', title: __('land_time'),
                            width:'80px',
                            operate:false
                        },
                        {field: 'start_construction_time', title: __('start_construction_time'),
                            addclass:'datetimerange',
                            datetimeFormat:"YYYY-MM",
                            formatter: Table.api.formatter.datetime,
                            visible:false
                        },
                        {field: 'start_construction_time_desc', title: __('start_construction_time'),
                            width:'80px',
                            operate:false
                        },
                        {field: 'investment_name',title: __('investment_name'),width:'100px',operate:'like',
                            formatter:Table.api.formatter.delimiter
                        },
                        {field: 'investment_area', title: __('investment_area'),operate:'like',
                            width:'60px',
                            formatter:Table.api.formatter.delimiter
                        },
                        {field: 'investment_attribute_ids', title: __('Investment_attribute_ids'),
                            visible:false,
                            searchList: $.getJSON("selectlist/index/tablename/investor_attribute")
                        },
                        {field: 'investment_attribute_names', title: __('Investment_attribute_ids'),
                            width:'80px',
                            operate:false,formatter:Table.api.formatter.delimiter
                        },
                        {field: 'settle_street_ids', title: __('Settle_street_ids'),
                            visible:false,
                            searchList: $.getJSON("selectlist/street")
                            // extend:"data-source='industry_category/index'"
                        },
                        {field: 'settle_street_names', title: __('Settle_street_ids'),
                            width:'80px',
                            operate:false,
                        },
                        {field: 'project_source_ids', title: __('project_source_ids'),
                            visible:false,
                            searchList: $.getJSON("selectlist/index/tablename/activity")
                        },
                        {field: 'project_source_names', title: __('project_source_ids'),
                            width:'80px',
                            operate:false,
                            formatter:Table.api.formatter.delimiter
                        },
                        {field: 'city_district_contact', title: __('City_district_contact'),width:"60px",operate:false},
                        {field: 'memo', title: __('备注'),width:"100px",operate:false},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add:function() {
            Controller.api.bindevent();
        },
        edit:function() {
            Controller.api.bindevent();
        },
        api:{
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $("#carrier_id").change(function(event) {

                    var value = $(this).val();
                    var id = $(this).attr('id');
                    // alert(id);
                    var unit = '';
                    var readonly = false;
                    var required = null;
                    switch (value) {
                        case "1":
                            unit = "亩";
                            readonly = false;
                            required = "required";
                            break;
                        case "2":
                        case "3":
                        case "4":
                            unit = "平米";
                            readonly = false;
                            required = "required";
                            break;
                        case "5":
                            unit = "";
                            readonly = true;
                            required = null;
                            break;
                    }

                    $("#carrier_id_addon").html(unit);
                    if(value == "5")
                    {
                        $("form[role=form]").validator("setField", "row[carrier_id_relation]", null);
                        $("#carrier_id_relation").removeAttr('aria-invalid');
                        $("#carrier_id_relation").attr('readonly','readonly');
                    }
                    else
                    {
                        $("form[role=form]").validator("setField", "row[carrier_id_relation]", required);
                        $("#carrier_id_relation").removeAttr('readonly')
                    }

                });
            // alert(pre)

                // Controller.api.showLand('0');
                $("#is_land").change(function(event) {
                    var value = $(this).val();
                    Controller.api.showLand(value);
                });
                $("#is_access").change(function(event) {
                    var value = $(this).val();
                    Controller.api.showAccess(value);
                });
                $(".pending").click(function(){
                    var item_id =$(this).data('id');
                    if(this.checked)
                    {
                        $("#"+item_id).val("待定");
                        $("#"+item_id).attr('readonly','readonly');
                    }
                    else
                    {
                        $("#"+item_id).val("");
                        $("#"+item_id).removeAttr('readonly');
                    }
                });
            },
            showLand:function(value){

                console.log(value);

                if(value == '1'){
                    Layer.alert('请填写拿地时间');

                    $("form[role=form]").validator("setField", {
                        "row[land_time]": "required;",
                    });

                    $(".div-land_time").show();
                }else{

                    $(".div-land_time").hide();

                    $("form[role=form]").validator("setField", {
                        "row[land_time]": null,
                    });


                    $(".land_time").removeAttr('aria-invalid');

                }
            },
            showAccess:function(value){

                console.log(value);

                if(value == '1'){
                    Layer.alert('请填写准入时间');

                    $("form[role=form]").validator("setField", {
                        "row[access_time]": "required;",
                    });

                    $(".div-access_time").show();
                }else{

                    $(".div-access_time").hide();

                    $("form[role=form]").validator("setField", {
                        "row[access_time]": null,
                    });


                    $(".access_time").removeAttr('aria-invalid');

                }
            },
        }
    }
    return Controller;
})